package com.yzr.common.collection.collections;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @description
 * @author: yx-0173
 * @date: 2021-08-11 15:35
 **/
public class CollectionUtils {

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("tom");
        list.add("jack");
        list.add("smith");

        // 反转list元素的顺序
//        reverse(list);
        // 随机排序
//        shuffle(list);
        // 自然排序
//        sort(list);
        // 交换
//        swap(list, 0, 1);
//        max(list, (k, v) -> 0);
        List<String> objects = new ArrayList<>(3);
        objects.add("fsfd");
        objects.add("fsfd");
        objects.add("fsfd");
        objects.add("fsfd");
        objects.add("fsfd");

        copy(list, objects);
        System.out.println(objects);
    }

    public static void reverse(List<?> list) {
        Collections.reverse(list);
    }

    public static void shuffle(List<?> list) {
        Collections.shuffle(list);
    }

    public static <T extends Comparable<? super T>> void sort(List<? extends T> list) {
        Collections.sort(list);
    }

    public static void swap(List<?> list, int from, int to) {
        Collections.swap(list, from, to);
    }

    public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> list, Comparator<? super T> comp) {
        return Collections.max(list);
    }

    /**
     *
     * @param from 源不能大于要拷贝的
     * @param to 拷贝
     * @param <T>
     */
    public static <T> void copy(List<? extends T> from, List<? super T> to) {
        Collections.copy(to, from);
    }

}
